<template>
  <div>
    <el-tree
      ref="tree"
      :data="permissionData"
      show-checkbox
      :props="{ label: 'name' }"
      node-key="id"
      :default-expand-all="true"
      :check-strictly="true"
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="Save">确定</el-button>
      <el-button @click="closeAssign">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permissionData: []
    }
  },
  mounted() {
    this.loadPermissionList() // 存储权限
    this.loadRoleDetailById() // 角色详情
  },
  methods: {
    // permIds -->getCheckedKeys
    async  Save() {
      const permIds = this.$refs.tree.getCheckedKeys()
      const res = await assignPerm({ id: this.roleId, permIds: permIds })
      console.log('保存角色权限点', res)
      this.closeAssign()
    },
    //   角色详情
    async   loadRoleDetailById() {
    //   alert(this.roleId)
      const res = await getRoleDetail(this.roleId)
      console.log('角色详情', res)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    // 获取列表
    async loadPermissionList() {
      const res = await getPermissionList()
      //   console.log('获取列表', res)
      this.permissionData = tranListToTreeData(res.data)
    //   console.log('树状', this.permissionData)
    },
    closeAssign() {
      // 通过父组件去关闭弹层
      this.$emit('close-assign')
      // 清空当前的选择
      this.$refs.tree.setCheckedKeys([])
    }
  }
}
</script>

<style>
</style>
